package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportProductVo;
import cn.itcast.vo.ExportResult;
import cn.itcast.vo.ExportVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class ExportServiceImpl implements ExportService {

 @Autowired
 private ExportDao exportDao;

 @Autowired
 private ContractDao contractDao;

 @Autowired
 private ContractProductDao contractProductDao;

 @Autowired
 private ExportProductDao exportProductDao;

 @Autowired
 private ExtCproductDao extCproductDao;

 @Autowired
 private ExtEproductDao extEproductDao;

 //根据id查询
 public Export findById(String id) {
  return exportDao.selectByPrimaryKey(id);
 }

 //保存
 public void save(Export export) {
  //根据勾选的合同生成报运单
  int proNum = 0;
  int extNum = 0;
  //设置id
  export.setId(UUID.randomUUID().toString());
  //根据勾选合同id，查询所有合同
  String[] cids = export.getContractIds().split(",");
  String customerContract = "";
  for (String cid : cids) {
   Contract contract = contractDao.selectByPrimaryKey(cid);
   //拼接合同
   customerContract += contract.getContractNo() + " ";
   //更新购销合同的状态
   contract.setState(2);
   contractDao.updateByPrimaryKeySelective(contract);
   proNum += contract.getProNum();
   extNum += contract.getExtNum();
  }
  export.setCustomerContract(customerContract);
  //根据勾选的合同货物生成报运单商品数据
  //根据合同id，查询所有的合同货物
  ContractProductExample cpe = new ContractProductExample();
  ContractProductExample.Criteria cc = cpe.createCriteria();
  cc.andContractIdIn(Arrays.asList(cids));
  List<ContractProduct> cps = contractProductDao.selectByExample(cpe);
  //循环所有的合同货物
  for (ContractProduct cp : cps) {
   //一个货物构造一个商品对象数据
   ExportProduct ep = new ExportProduct();
   //将cp中同名属性赋值到ep对象上
   BeanUtils.copyProperties(cp,ep);
   //设置商品id
   ep.setId(UUID.randomUUID().toString());
   //设置报运单id
   ep.setExportId(export.getId());
   //保存
   exportProductDao.insertSelective(ep);
   //根据勾选的合同附件生成报运单附件数据
   //根据当前合同货物的id，查询此货物的所有附件
   ExtCproductExample example = new ExtCproductExample();
   ExtCproductExample.Criteria criteria = example.createCriteria();
   criteria.andContractProductIdEqualTo(cp.getId());
   List<ExtCproduct> ecps = extCproductDao.selectByExample(example);
   //循环此合同货物的所有附件
   for (ExtCproduct ecp : ecps) {
    //一个合同附件，生成一个报运单附件
    ExtEproduct eep = new ExtEproduct();
    BeanUtils.copyProperties(ecp,eep);
    //设置附件id
    eep.setId(UUID.randomUUID().toString());
    //设置报运单id
    eep.setExportId(export.getId());
    //设置商品id
    eep.setExportProductId(ep.getId());
    //保存到数据库中
    extEproductDao.insertSelective(eep);
   }
  }
  export.setProNum(proNum);
  export.setExtNum(extNum);
  export.setState(0);
  exportDao.insertSelective(export);
 }

 //更新
 public void update(Export export) {
  //更新报运单
  exportDao.updateByPrimaryKeySelective(export);
  //循环报运单下更新的所有商品，更新每个商品
  if (export.getExportProducts() != null) {
   for (ExportProduct ep : export.getExportProducts()) {
    exportProductDao.updateByPrimaryKeySelective(ep);
   }
  }
 }

 //删除
 public void delete(String id) {

 }

 //分页查询
 public PageInfo findAll(ExportExample example, int page, int size) {
  PageHelper.startPage(page, size);
  List<Export> list = exportDao.selectByExample(example);
  return new PageInfo(list);
 }

 //海关保运方法
 public void exportE(String id) {
  //根据id查询报运单
  Export export = exportDao.selectByPrimaryKey(id);
  //根据报运单id查询报运单商品列表
  ExportProductExample example = new ExportProductExample();
  ExportProductExample.Criteria criteria = example.createCriteria();
  criteria.andExportIdEqualTo(id);
  List<ExportProduct> eps = exportProductDao.selectByExample(example);
  //内部的Exprot对象转化为海关保运的ExportVo对象
  ExportVo evo = new ExportVo();
  BeanUtils.copyProperties(example,evo);
  //设置报运单id
  evo.setExportId(export.getId());
  //ExportProduct对象转化为海关保运的ExportProductVo对象
  List<ExportProductVo> epvos = new ArrayList<>();
  for (ExportProduct ep : eps) {
   ExportProductVo epvo = new ExportProductVo();
   BeanUtils.copyProperties(ep, epvo);
   epvo.setExportProductId(ep.getId());
   epvos.add(epvo);
  }
  evo.setProducts(epvos);
  //通过WebClient工具类发送数据
  WebClient wc = WebClient.create("http://localhost:9001/ws/export/ep");
  Integer res = wc.post(evo, Integer.class);
  //根据海关平台的响应数据，更新报运单状态
  if (res == 0) {
   export.setState(2);
   exportDao.updateByPrimaryKeySelective(export);
  }
 }

 //查询所有报运单
 public List<Export> findAll(ExportExample example) {
  return exportDao.selectByExample(example);
 }

 //更新保运结果
 public void updateE(ExportResult result) {
  //查询报运单
  Export export = exportDao.selectByPrimaryKey(result.getExportId());
  //更新报运单数聚
  export.setState(result.getState());
  export.setRemark(result.getRemark());
  exportDao.updateByPrimaryKeySelective(export);
  //循环报运单所有商品
  for (ExportProductResult product : result.getProducts()) {
   ExportProduct exportProduct = exportProductDao.selectByPrimaryKey(product.getExportProductId());
   exportProduct.setTax(product.getTax());
   //更新报运单税
   exportProductDao.updateByPrimaryKeySelective(exportProduct);
  }

 }
}
